package DAO;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class CalendarPick {

    private JButton[] but;
    private JPanel panel1, panel2;
    private JDialog dialog;
    private String day;
    private int month;
    private int year;
    JLabel lbDate;
    private String date;
    private String dateNow;
    private Date dateChoose;

    public CalendarPick() {
        dialog = new JDialog();
        dialog.setModal(true);
        but = new JButton[49];
        month = Calendar.getInstance().get(Calendar.MONTH);
        year = Calendar.getInstance().get(Calendar.YEAR);
        makeBut();
        addPanel1();
        addPanel2();
        makeCalendar();
        setDayPresent();
        actionButtonDate();
        setEnableButton();  // Ẩn các nút không có ngày.

        dialog.add(panel1);
        dialog.add(panel2, BorderLayout.NORTH);
        dialog.pack();
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);
    }

    public void setEnableButton() {
        for (int i = 7; i < but.length; i++) {
            if (but[i].getText().equals("")) {
                but[i].setEnabled(false);
            } else {
                but[i].setEnabled(true);
            }
        }
    }

    public Date getDateChoose() {
        return dateChoose;
    }

    public void setDateChoose(Date dateChoose) {
        this.dateChoose = dateChoose;
    }

    private void makeBut() {
        for (int i = 0; i < but.length; i++) {
            but[i] = new JButton();
            but[i].setText("");
        }

        String[] dayName = {"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"};
        for (int i = 0; i < 7; i++) {
            but[i].setForeground(Color.red);
            but[i].setText(dayName[i]);
        }
    }

    private void addPanel1() {
        panel1 = new JPanel(new GridLayout(7, 7));
        panel1.setPreferredSize(new Dimension(370, 150));

        for (JButton b : but) {
            panel1.add(b);
        }
    }

    private void addPanel2() {
        panel2 = new JPanel(new GridLayout(1, 3));
        JButton btNext = new JButton(">");
        JButton btPrev = new JButton("<");
        lbDate = new JLabel("", JLabel.CENTER);

        actionNextMonth(btNext);
        actionPreviousMonth(btPrev);

        panel2.add(btPrev);
        panel2.add(lbDate);
        panel2.add(btNext);
    }

    private void actionNextMonth(JButton btNext) {
        btNext.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                month++;
                makeCalendar();
                setEnableButton();
                setDayPresent();
            }
        });

    }

    private void actionPreviousMonth(JButton btPrev) {
        btPrev.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                month--;
                makeCalendar();
                setEnableButton();
                setDayPresent();
            }
        });
    }

    private void makeCalendar() {
        for (int i = 7; i < but.length; i++) {
            but[i].setText("");
        }
        SimpleDateFormat symFormat = new SimpleDateFormat("MMMM yyyy");
        Calendar cal = Calendar.getInstance();

        cal.set(year, month, 1);
        int DoW = cal.get(Calendar.DAY_OF_WEEK);
        int DoM = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

        for (int i = 6 + DoW, day = 1; day <= DoM; day++, i++) {
            but[i].setBackground(Color.white);
            but[i].setText("" + day);
        }


        lbDate.setText(symFormat.format(cal.getTime()));
    }

    private void actionButtonDate() {
        for (int i = 7; i < but.length; i++) {
            final int selected = i;
            but[i].addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    day = but[selected].getActionCommand();
                    dialog.dispose();
                }
            });
        }
    }

    public String getSelectDate() {
        try {
            if (day.equals("")) {
                return day;
            }

            Calendar cal = Calendar.getInstance();
            cal.set(year, month, Integer.parseInt(day));
            SimpleDateFormat symFormat = new SimpleDateFormat("yyyy/MM/dd");
            date = symFormat.format(cal.getTime());
            return date;
        } catch (Exception e) {
            System.out.println("Not seclect any day");
            return "";
        }
        
    }

    private void setDayPresent() {
        Date d = new Date();
        SimpleDateFormat f = new SimpleDateFormat("yyyy/MM/dd");
        dateNow = f.format(d);
        Calendar cal = Calendar.getInstance();

        for (int i = 7; i < but.length; i++) {
            if (but[i].getText().trim().equals("")) {
                return;
            }
            int dateInCal = Integer.parseInt(but[i].getText().trim());
            cal.set(year, month, dateInCal);
            if (dateNow.equals(f.format(cal.getTime()))) {
                but[i].setBackground(Color.red);
            }
        }
    }
}
